<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "set_user_up".
 *
 * @property int $id
 * @property float $vip1 一次性购买, 成为会员
 * @property int $vip2 直推几个会员, 成为分销商
 * @property int $vip3 团队多少人, 成为总经销, 包含本人
 * @property float $vip1_recommend
 * @property float $vip2_recommend
 * @property float $vip2_second
 * @property float $repeat_vip1_recommend
 * @property float $repeat_vip2_second
 * @property float $equal_1
 * @property float $equal_2
 * @property int $vip3_1
 * @property int $vip3_2
 * @property float $profit
 * @property float $province
 * @property float $city
 * @property float $area
 * @property float $vip3_price
 * @property float $vip3_recommend
 * @property float $vip3_recommend_2
 */
class SetUserUp extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'set_user_up';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['vip1', 'vip1_recommend', 'vip2_recommend', 'vip2_second', 'repeat_vip1_recommend', 'repeat_vip2_second', 'equal_1', 'equal_2', 'profit', 'province', 'city', 'area'], 'number'],
            [['vip2', 'vip3', 'vip3_1', 'vip3_2'], 'integer'],

            [['vip3_price', 'vip3_recommend', 'vip3_recommend_2'], 'safe']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'vip1' => 'Vip1',
            'vip2' => 'Vip2',
            'vip3' => 'Vip3',
            'vip1_recommend' => 'Vip1 Recommend',
            'vip2_recommend' => 'Vip2 Recommend',
            'vip2_second' => 'Vip2 Second',
            'repeat_vip1_recommend' => 'Repeat Vip1 Recommend',
            'repeat_vip2_second' => 'Repeat Vip2 Second',
            'equal_1' => 'Equal 1',
            'equal_2' => 'Equal 2',
            'vip3_1' => 'Vip3 1',
            'vip3_2' => 'Vip3 2',
            'profit' => 'Profit',
            'province' => 'Province',
            'city' => 'City',
            'area' => 'Area',
        ];
    }

    public static function getLastOne()
    {
        $model = self::find()
            ->orderBy(['id' => SORT_DESC])
            ->one();

        if (!$model) {
            $model = new self();
        }

        return $model;
    }

    public static function saveData($data)
    {
        $model = self::getLastOne();

        if ($model->load(['SetUserUp' => $data]) && $model->save()) {
            return true;
        }
    }
}
